﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using iuPlace.Web.Extensions;

namespace iuPlace.Web
{
    public class iuPlaceApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute("Default", "", new { controller = "Default", action = "Index" }, new string[] { "iuPlace.Web.Controllers" });

            routes.MapRoute("Mobile", "Mobile", new { controller = "Default", action = "Mobile" }, new string[] { "iuPlace.Web.Controllers" });
            routes.MapRoute("Site", "Site", new { controller = "Default", action = "Site" }, new string[] { "iuPlace.Web.Controllers" });          
            
            routes.MapRoute("CacheMobile", "Cache/Mobile", new { controller = "Cache", action = "Mobile" }, new string[] { "iuPlace.Web.Controllers" });
            routes.MapRoute("CacheSite", "Cache/Site", new { controller = "Cache", action = "Site" }, new string[] { "iuPlace.Web.Controllers" });
        }

        public static iuPlaceApplication Current
        {
            get { return System.Web.HttpContext.Current.ApplicationInstance as iuPlaceApplication; }
        }

        public iuPlace.Core.Session CurrentSession
        {
            get { return (iuPlace.Core.Session)this.Session["CurrentSession"]; }
            set { this.Session["CurrentSession"] = value; }
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();           
            Database.DefaultConnectionFactory = new SqlConnectionFactory("Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True");

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes); 
     
            BundleTable.Bundles.RegisterSiteBundles();
            BundleTable.Bundles.RegisterMobileBundles();
            BundleTable.Bundles.RegisterApplicationBundles();
            iuPlace.Core.Application.Initialize();
        }

        protected void Session_Start(Object sender, EventArgs e)
        {
            base.Session.Timeout = 2;
            System.Web.Security.FormsAuthentication.SignOut();
            this.CurrentSession = iuPlace.Core.Application.NewSession(this.Session.SessionID);
        }

        protected void Session_End()
        {
            if (this.CurrentSession.IsAuthenticated)
            {
                using (var service = this.CurrentSession.ExecuteService<iuPlace.Core.Services.UserService>())
                {
                    service.Logout();
                }                
            }
            iuPlace.Core.Application.RemoveSession(this.Session.SessionID);
        }
    }
}